package com.masterclient.app.modules.goods.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.goods.ShopBean;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：sean
 * 创建时间：2017/6/27 13:32
 * 功能说明：
 */

public class NewGoodPagerAdapter extends PagerAdapter {


    List<ShopBean.DataBean.ShNewProductsBean> list;
    private Context context;

    public NewGoodPagerAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<ShopBean.DataBean.ShNewProductsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final ShopBean.DataBean.ShNewProductsBean bean = list.get(position);
        LinearLayout linearLayout = (LinearLayout) View.inflate(context, R.layout.item_new_good, null);
        ViewHolder holder = new ViewHolder(linearLayout);
        App.picasso.load(bean.getSh_image()).fit().into(holder.ivGood);
        holder.tvGoodName.setText(bean.getSh_name());
        holder.tvPrice.setText("¥"+bean.getSh_show_price());
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                        .withString("id", bean.getSh_id())
                        .navigation();

                MobclickAgent.onEvent(context,"click_shop_recommend");
            }
        });

        TextView tv_line_price = (TextView) linearLayout.findViewById(R.id.tv_line_price);
        if(TextUtils.isEmpty(bean.getSh_line_price())){
            tv_line_price.setVisibility(View.GONE);
        }else {
            tv_line_price.setVisibility(View.VISIBLE);
            tv_line_price.setText("¥"+bean.getSh_line_price());
            tv_line_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
        }

        LinearLayout ll_user = (LinearLayout) linearLayout.findViewById(R.id.ll_user);
        LinearLayout ll_earn = (LinearLayout) linearLayout.findViewById(R.id.ll_earn);
        TextView tv_price_n = (TextView) linearLayout.findViewById(R.id.tv_price_n);
        TextView tv_money_earn = (TextView) linearLayout.findViewById(R.id.tv_money_earn);
        if(0 == Double.valueOf(bean.getSh_partner_commission())){
            ll_earn.setVisibility(View.GONE);
            ll_user.setVisibility(View.VISIBLE);
        }else {
            ll_earn.setVisibility(View.VISIBLE);
            ll_user.setVisibility(View.GONE);
            tv_price_n.setText("¥"+bean.getSh_show_price());
            tv_money_earn.setText("赚"+bean.getSh_partner_commission());
        }


        container.addView(linearLayout);
        return linearLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    static class ViewHolder {
        @BindView(R.id.iv_good)
        ImageView ivGood;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
